import http from "@/api";

// 物料相关接口
export namespace Material {
  // 创建物料请求参数
  export interface CreateMaterialRequest {
    materialCode: string;
    materialName: string;
    specification?: string;
    model?: string;
    unit: string;
    groupId: string;
    standardCost?: number;
    description?: string;
  }

  // 更新物料请求参数
  export interface UpdateMaterialRequest {
    materialName: string;
    specification?: string;
    model?: string;
    unit: string;
    groupId: string;
    standardCost?: number;
    description?: string;
  }

  // 物料列表项
  export interface MaterialListItem {
    id: string;
    materialCode: string;
    materialName: string;
    specification?: string;
    model?: string;
    unit: string;
    groupId: string;
    groupName?: string;
    standardCost?: number;
    status: string;
    description?: string;
    createdBy?: string;
    createdTime: string;
    updatedBy?: string;
    updatedTime: string;
  }

  // 物料列表响应（MyBatis-Plus 分页结构）
  export interface MaterialListResponse {
    records: MaterialListItem[];
    total: number;
    size: number;
    current: number;
    pages: number;
  }
}

/** 创建物料 */
export const createMaterial = (params: Material.CreateMaterialRequest) => {
  return http.post<Long>("/materials/create", params);
};

/** 获取物料列表 */
export const getMaterialList = (params?: {
  materialCode?: string;
  materialName?: string;
  groupId?: string;
  status?: string;
  current?: number;
  size?: number;
}) => {
  return http.get<Material.MaterialListResponse>("/materials/list", params);
};

/** 更新物料 */
export const updateMaterial = (id: string, params: Material.UpdateMaterialRequest) => {
  return http.put<void>(`/materials/${id}`, params);
};

/** 删除物料 */
export const deleteMaterial = (id: string) => {
  return http.delete<void>(`/materials/${id}`);
};

/** 批量删除物料 */
export const batchDeleteMaterial = (ids: string[]) => {
  return http.delete<void>("/materials/batch", { data: ids });
};

/** 获取物料详情 */
export const getMaterialById = (id: string) => {
  return http.get<Material.MaterialListItem>(`/materials/${id}`);
};

/** 启用物料 */
export const enableMaterial = (id: string) => {
  return http.put<void>(`/materials/${id}/enable`);
};

/** 禁用物料 */
export const disableMaterial = (id: string) => {
  return http.put<void>(`/materials/${id}/disable`);
};

/** 根据群组获取物料列表 */
export const getMaterialsByGroupId = (groupId: string) => {
  return http.get<Material.MaterialListItem[]>(`/materials/group/${groupId}`);
};

/** 获取启用状态的物料列表 */
export const getActiveMaterials = () => {
  return http.get<Material.MaterialListItem[]>("/materials/active");
};